package com.glory.gloryquickui.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.glory.gloryquickui.R;


/**
 * @date 2017/9/12
 * @desc
 */

public class FreeApi_ToastUtils {

    /**
     * 显示toast
     */
    private static Toast toast;
    private static View toastRoot;
    private static TextView mTextView;
    private static ImageView imageView;

    public static void showFreeToast(String content, Context context,
                                     boolean isSuccess, int duration) {
        int successID =  R.drawable.free_ui_glory_success;
        int failID = R.drawable.free_ui_glory_fail;
        int resID = R.layout.free_ui_blue_sky_toast_genaral;
        if (toast == null) {
            initBottomView(context, resID);
            toast = new Toast(InitUtils.Companion.getApp());
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        mTextView.setText(content);
        if (isSuccess) {
            imageView.setImageResource(successID);
        } else {
            imageView.setImageResource(failID);
        }
        toast.setDuration(duration);
        toast.setView(toastRoot);
        toast.show();
    }

    private static void initBottomView(Context context, int resID) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                (int) (InitUtils.Companion.getSCREEN_WIDTH() * 0.3),
                ViewGroup.LayoutParams.WRAP_CONTENT);
        //加载Toast布局
        toastRoot = LayoutInflater.from(context).inflate(resID, null);
        //初始化布局控件
        mTextView = toastRoot.findViewById(R.id.tvTips);
        mTextView.setLayoutParams(layoutParams);
        //初始化布局控件
        imageView = toastRoot.findViewById(R.id.ivStatus);
    }

    public static void cancel() {
        if (toast != null) {
            toast.cancel();
        }
    }
}
